/*
 * PanelPathEditor.java
 *
 * Created on December 8, 2007, 11:36 AM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package hextd.UI;

import hextd.hex.Hex;
import hextd.util.Context;
import hextd.wave.Path;
import hextd.wave.PathNormal;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author  hylke
 */
public class PanelPathEditor extends javax.swing.JPanel {
    
    private DefaultListModel modelPathSelector;
    private DefaultListModel modelPathContents;
    boolean stepInsert = true;
    private Context context;
    
    /** Creates new form PanelPathEditor */
    public PanelPathEditor() {
    }
    
    public void setContext(Context c) {
        this.context = c;
        initComponents();
        this.setVisible(false);
        this.modelPathSelector = (DefaultListModel)this.jList_pathSelector.getModel();
        this.modelPathContents = (DefaultListModel)this.jList_pathContent.getModel();
    }
    
    public void resetPaths() {
        this.modelPathSelector.clear();
        this.modelPathContents.clear();
        Vector<Path> paths = this.context.getPaths();
        for (int i=0; i<paths.size(); i++) {
            this.modelPathSelector.add(i, "Path "+(i+1)+" ("+paths.get(i).length()+")");
        }
        this.invalidate();
    }
    
    public void selectPath(int s) {
        Vector<Path> paths = this.context.getPaths();
        for (int i=0; i<paths.size(); i++) {
            paths.get(i).setSelected(false);
        }
        if (s>=0 && s< paths.size()) {
            this.modelPathContents.clear();
            Path tempPath = paths.get(s);
            tempPath.setSelected(true);
            for (int i=0; i<tempPath.length();i++) {
                int [] tempStep = tempPath.getStepHexCoords(i);
                String step = ""+tempStep[0]+","+tempStep[1];
                this.modelPathContents.add(i, step);
            }
            this.jSpinner_delay.setValue(tempPath.getBaseDelay());
        }
        this.invalidate();
    }
    
    private void addPath() {
        Vector<Path> paths = this.context.getPaths();
        PathNormal newPath = new PathNormal(this.context, 0);
        newPath.finalise();
        newPath.resetScale();
        paths.add(newPath);
        this.resetPaths();
        this.selectPath(paths.size()-1);
    }
    
    private void readDelay() {
        Vector<Path> paths = this.context.getPaths();
        int pathNr = this.jList_pathSelector.getSelectedIndex();
        if (pathNr >=0 && pathNr < paths.size()) {
            PathNormal p = (PathNormal) paths.get(pathNr);
            try {
                p.setBaseDelay(new Float(this.jSpinner_delay.getValue().toString()).floatValue());
            } catch (NumberFormatException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    private void removePath() {
        Vector<Path> paths = this.context.getPaths();
        int pathNr = this.jList_pathSelector.getSelectedIndex();
        if (pathNr >=0 && pathNr < paths.size()) {
            paths.remove(pathNr);
            this.resetPaths();
        }
    }
    
    public void hexClicked(Hex hex) {
        int pathNr = this.jList_pathSelector.getSelectedIndex();
        int stepNr = this.jList_pathContent.getSelectedIndex();
        Vector<Path> paths = this.context.getPaths();
        
        if (pathNr >=0 && pathNr < paths.size() && stepNr <= paths.get(pathNr).length()) {
            int[] l = hex.getLocation();
            PathNormal p = (PathNormal) paths.get(pathNr);
            if (this.stepInsert) {
                p.insertStep(stepNr+1, l[0], l[1]);
                stepNr++;
            } else {
                if (stepNr >= 0) {
                    p.updateStep(stepNr, l[0], l[1]);
                }
            }
            this.selectPath(pathNr);
            this.jList_pathContent.setSelectedIndex(stepNr);
        }
    }
    
    private void removeStep() {
        Vector<Path> paths = this.context.getPaths();
        int pathNr = this.jList_pathSelector.getSelectedIndex();
        int stepNr = this.jList_pathContent.getSelectedIndex();
        if (pathNr >=0 && pathNr < paths.size() && stepNr>=0 && stepNr < paths.get(pathNr).length()) {
            PathNormal p = (PathNormal) paths.get(pathNr);
            p.removeStep(stepNr);
            this.selectPath(pathNr);
            this.jList_pathContent.setSelectedIndex(stepNr-1);
        }
    }
    
    private void selectStep() {
        Vector<Path> paths = this.context.getPaths();
        int pathNr = this.jList_pathSelector.getSelectedIndex();
        int stepNr = this.jList_pathContent.getSelectedIndex();
        if (pathNr >=0 && pathNr < paths.size() && stepNr>=0 && stepNr <= paths.get(pathNr).length()) {
            int [] tempStep = paths.get(pathNr).getStepHexCoords(stepNr);
            this.context.highlightHex(tempStep);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jButton_pathMin = new javax.swing.JButton();
        jButton_pathPlus = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList_pathSelector = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList_pathContent = new javax.swing.JList();
        jButton_contentMin = new javax.swing.JButton();
        jToggleButton_contentMode = new javax.swing.JToggleButton();
        jSpinner_delay = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();

        setBackground(this.context.colors.getColorBackGround());
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Paths", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Bitstream Vera Sans", 0, 13), this.context.colors.getColorForeGround()));
        setForeground(this.context.colors.getColorForeGround());
        setMaximumSize(new java.awt.Dimension(200, 2147483647));
        setMinimumSize(new java.awt.Dimension(200, 63));
        setPreferredSize(new java.awt.Dimension(200, 289));
        setLayout(new java.awt.GridBagLayout());

        jSplitPane1.setBackground(this.context.colors.getColorBackGround());
        jSplitPane1.setForeground(this.context.colors.getColorForeGround());
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setResizeWeight(0.3);

        jPanel1.setBackground(this.context.colors.getColorBackGround());
        jPanel1.setForeground(this.context.colors.getColorForeGround());
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jButton_pathMin.setBackground(this.context.colors.getColorBackGround());
        jButton_pathMin.setForeground(this.context.colors.getColorForeGround());
        jButton_pathMin.setText("-");
        jButton_pathMin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pathMinActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 2);
        jPanel1.add(jButton_pathMin, gridBagConstraints);

        jButton_pathPlus.setBackground(this.context.colors.getColorBackGround());
        jButton_pathPlus.setForeground(this.context.colors.getColorForeGround());
        jButton_pathPlus.setText("+");
        jButton_pathPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pathPlusActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
        jPanel1.add(jButton_pathPlus, gridBagConstraints);

        jScrollPane2.setBackground(this.context.colors.getColorBackGround());
        jScrollPane2.setForeground(this.context.colors.getColorForeGround());

        jList_pathSelector.setBackground(this.context.colors.getColorBackGround());
        jList_pathSelector.setForeground(this.context.colors.getColorForeGround());
        jList_pathSelector.setModel(new DefaultListModel());
        jList_pathSelector.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList_pathSelector.setVisibleRowCount(3);
        jList_pathSelector.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_pathSelectorValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList_pathSelector);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel1.add(jScrollPane2, gridBagConstraints);

        jSplitPane1.setTopComponent(jPanel1);

        jPanel2.setBackground(this.context.colors.getColorBackGround());
        jPanel2.setForeground(this.context.colors.getColorForeGround());
        jPanel2.setLayout(new java.awt.GridBagLayout());

        jScrollPane3.setBackground(this.context.colors.getColorBackGround());
        jScrollPane3.setForeground(this.context.colors.getColorForeGround());

        jList_pathContent.setBackground(this.context.colors.getColorBackGround());
        jList_pathContent.setForeground(this.context.colors.getColorForeGround());
        jList_pathContent.setModel(new DefaultListModel());
        jList_pathContent.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList_pathContent.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_pathContentValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(jList_pathContent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.03;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        jPanel2.add(jScrollPane3, gridBagConstraints);

        jButton_contentMin.setBackground(this.context.colors.getColorBackGround());
        jButton_contentMin.setForeground(this.context.colors.getColorForeGround());
        jButton_contentMin.setText("-");
        jButton_contentMin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_contentMinActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        jPanel2.add(jButton_contentMin, gridBagConstraints);

        jToggleButton_contentMode.setBackground(this.context.colors.getColorBackGround());
        jToggleButton_contentMode.setForeground(this.context.colors.getColorForeGround());
        jToggleButton_contentMode.setText("Insert");
        jToggleButton_contentMode.setContentAreaFilled(false);
        jToggleButton_contentMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton_contentModeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.01;
        jPanel2.add(jToggleButton_contentMode, gridBagConstraints);

        jSpinner_delay.setBackground(this.context.colors.getColorBackGround());
        jSpinner_delay.setForeground(this.context.colors.getColorForeGround());
        jSpinner_delay.setModel(new SpinnerNumberModel(0,0,20,0.1));
        jSpinner_delay.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner_delayStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jSpinner_delay, gridBagConstraints);

        jLabel1.setBackground(this.context.colors.getColorBackGround());
        jLabel1.setForeground(this.context.colors.getColorForeGround());
        jLabel1.setText("Delay:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jLabel1, gridBagConstraints);

        jSplitPane1.setRightComponent(jPanel2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0E-4;
        gridBagConstraints.weighty = 0.05;
        add(jSplitPane1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    private void jSpinner_delayStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner_delayStateChanged
        this.readDelay();
    }//GEN-LAST:event_jSpinner_delayStateChanged
    
    private void jButton_pathMinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pathMinActionPerformed
        this.removePath();
    }//GEN-LAST:event_jButton_pathMinActionPerformed
    
    private void jButton_pathPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pathPlusActionPerformed
        this.addPath();
    }//GEN-LAST:event_jButton_pathPlusActionPerformed
    
    private void jList_pathContentValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_pathContentValueChanged
        this.selectStep();
    }//GEN-LAST:event_jList_pathContentValueChanged
    
    private void jButton_contentMinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_contentMinActionPerformed
        this.removeStep();
    }//GEN-LAST:event_jButton_contentMinActionPerformed
    
    private void jToggleButton_contentModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton_contentModeActionPerformed
        this.stepInsert = !jToggleButton_contentMode.isSelected();
        if (this.stepInsert) {
            this.jToggleButton_contentMode.setText("Insert");
        } else {
            this.jToggleButton_contentMode.setText("Move");
        }
    }//GEN-LAST:event_jToggleButton_contentModeActionPerformed
    
    private void jList_pathSelectorValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_pathSelectorValueChanged
        this.selectPath(this.jList_pathSelector.getSelectedIndex());
    }//GEN-LAST:event_jList_pathSelectorValueChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_contentMin;
    private javax.swing.JButton jButton_pathMin;
    private javax.swing.JButton jButton_pathPlus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList_pathContent;
    private javax.swing.JList jList_pathSelector;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSpinner jSpinner_delay;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToggleButton jToggleButton_contentMode;
    // End of variables declaration//GEN-END:variables
    
}
